package com.wh.wisdomsite.xbox.utils.properties;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;



/**
 * 读取Properties配置文件信息
 * @Package com.wh.zsd.util
 * @author 谢泽鹏
 * @date 2014年10月14日 下午2:00:50
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class PropertiesToolMgr {


    private static final Log logger = LogFactory.getLog(PropertiesToolMgr.class);
    
	//声明Properties对象
	private static Properties properties = null;
	
	//声明InputStream对象
	private static InputStream in = null;
	
	public static String fileName;


    /**
	 * 初始化配置文件
	 * @return Properties
	 */
	public static Properties initProperties(ClassLoader classLoader) {
		try {
			if (null == properties) {
				properties = new Properties();
			}
			in = (InputStream) classLoader.getResourceAsStream(getFileName());
			//in = new BufferedInputStream(new FileInputStream(fileName));
			properties.load(in);
			return properties;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("initProperties->>初始化配置文件错误！"  + e.getMessage());
			return null;
		}
	}
	
	/**
	 * 读取配置文件Key获取Value
	 * @param key Key值
	 * @return String  返回值
	 */
	public static String getPropertiesByKey(String key) {
		String value = null;
		try {
			value = properties.getProperty(key);
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("getPropertiesByKey->>根据配置文件Key获取Value错误！"  + e.getMessage());
		}
			return value;
	}
	

	/**
	 * 读取所有Key和Value
	 * @return map Map集合
	 */
	public static Map<String, String> getPropertiesAll(){
		Map<String, String> map = null;
		try {
			Enumeration<?> enumeration = properties.propertyNames();
			map = new HashMap<String, String>();
			while(enumeration.hasMoreElements()) {
				 String key = (String) enumeration.nextElement();
				 String value = properties.getProperty(key);
				 map.put(key, value);
		     }
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("getPropertiesAll->>读取所有KEY和VALUES"  + e.getMessage());
		}
			return map;
	}

    public static String getFileName() {
        return fileName;
    }

    public static void setFileName(String fileName) {
        PropertiesToolMgr.fileName = fileName;
    }
}
